﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;
namespace Human
{
	/// <summary>
	/// Summary description for frmInLyLich.
	/// </summary>
	public class frmInLyLich : System.Windows.Forms.Form
	{
        private AccessData mDB = new AccessData();
        Language lan = new Language();
		private DataSet ds =new DataSet();
        string sql = "",m_user="",denngay="",s_mmyy="",nguoilb="",s_bophan="",user_dn="";
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox ckbToanCongTy;
        private System.Windows.Forms.Button butXem;
		private System.Windows.Forms.ComboBox cbobBoPhan;
		private System.Windows.Forms.Button butIn;
		private System.Windows.Forms.Button butThoat;
		private bool bClear=true;
        //private Print rpt=new Print();
        private Button btnexel;
        private Label label3;
        private DateTimePicker dtp_denngay;
        private ComboBox cbo_hoten;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmInLyLich(string s_makp,string s_userdn)
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
            s_bophan = s_makp;
            user_dn = s_userdn;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmInLyLich));
            this.ckbToanCongTy = new System.Windows.Forms.CheckBox();
            this.cbobBoPhan = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.butThoat = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butXem = new System.Windows.Forms.Button();
            this.btnexel = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.dtp_denngay = new System.Windows.Forms.DateTimePicker();
            this.cbo_hoten = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // ckbToanCongTy
            // 
            this.ckbToanCongTy.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ckbToanCongTy.ForeColor = System.Drawing.Color.Blue;
            this.ckbToanCongTy.Location = new System.Drawing.Point(82, 99);
            this.ckbToanCongTy.Name = "ckbToanCongTy";
            this.ckbToanCongTy.Size = new System.Drawing.Size(104, 24);
            this.ckbToanCongTy.TabIndex = 3;
            this.ckbToanCongTy.Text = "Toàn đơn vị";
            this.ckbToanCongTy.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.ckbToanCongTy.CheckedChanged += new System.EventHandler(this.ckbToanCongTy_CheckedChanged);
            this.ckbToanCongTy.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ckbToanCongTy_KeyDown);
            // 
            // cbobBoPhan
            // 
            this.cbobBoPhan.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cbobBoPhan.Location = new System.Drawing.Point(80, 48);
            this.cbobBoPhan.Name = "cbobBoPhan";
            this.cbobBoPhan.Size = new System.Drawing.Size(202, 24);
            this.cbobBoPhan.TabIndex = 1;
            this.cbobBoPhan.SelectedIndexChanged += new System.EventHandler(this.cbobBoPhan_SelectedIndexChanged);
            this.cbobBoPhan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cbobBoPhan_KeyDown);
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(18, 50);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(61, 16);
            this.label1.TabIndex = 6;
            this.label1.Text = "Bộ phận:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(29, 78);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(50, 16);
            this.label2.TabIndex = 7;
            this.label2.Text = "Họ tên:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butThoat
            // 
            this.butThoat.BackColor = System.Drawing.SystemColors.Control;
            this.butThoat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThoat.Image = global::Human.Properties.Resources.close_r;
            this.butThoat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThoat.Location = new System.Drawing.Point(220, 136);
            this.butThoat.Name = "butThoat";
            this.butThoat.Size = new System.Drawing.Size(60, 28);
            this.butThoat.TabIndex = 5;
            this.butThoat.Text = "Thoát";
            this.butThoat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butThoat.UseVisualStyleBackColor = true;
            this.butThoat.Click += new System.EventHandler(this.butThoat_Click);
            // 
            // butIn
            // 
            this.butIn.BackColor = System.Drawing.SystemColors.Control;
            this.butIn.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butIn.Image = global::Human.Properties.Resources.t_printer;
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(88, 136);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(60, 28);
            this.butIn.TabIndex = 5;
            this.butIn.Text = "In";
            this.butIn.UseVisualStyleBackColor = true;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butXem
            // 
            this.butXem.BackColor = System.Drawing.SystemColors.Control;
            this.butXem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butXem.Image = global::Human.Properties.Resources.t_search;
            this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXem.Location = new System.Drawing.Point(23, 136);
            this.butXem.Name = "butXem";
            this.butXem.Size = new System.Drawing.Size(60, 28);
            this.butXem.TabIndex = 4;
            this.butXem.Text = "Xem";
            this.butXem.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butXem.UseVisualStyleBackColor = true;
            this.butXem.Click += new System.EventHandler(this.butXem_Click);
            // 
            // btnexel
            // 
            this.btnexel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btnexel.Image = global::Human.Properties.Resources.white_excel;
            this.btnexel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.btnexel.Location = new System.Drawing.Point(154, 136);
            this.btnexel.Name = "btnexel";
            this.btnexel.Size = new System.Drawing.Size(60, 28);
            this.btnexel.TabIndex = 5;
            this.btnexel.Text = "Exel";
            this.btnexel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.btnexel.UseVisualStyleBackColor = true;
            this.btnexel.Click += new System.EventHandler(this.btnexel_Click);
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(11, 21);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(68, 16);
            this.label3.TabIndex = 6;
            this.label3.Text = "Đến ngày:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // dtp_denngay
            // 
            this.dtp_denngay.CustomFormat = "dd/MM/yyyy";
            this.dtp_denngay.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dtp_denngay.Location = new System.Drawing.Point(80, 21);
            this.dtp_denngay.Name = "dtp_denngay";
            this.dtp_denngay.Size = new System.Drawing.Size(104, 20);
            this.dtp_denngay.TabIndex = 0;
            this.dtp_denngay.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dtp_denngay_KeyDown);
            // 
            // cbo_hoten
            // 
            this.cbo_hoten.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cbo_hoten.Location = new System.Drawing.Point(80, 76);
            this.cbo_hoten.Name = "cbo_hoten";
            this.cbo_hoten.Size = new System.Drawing.Size(202, 24);
            this.cbo_hoten.TabIndex = 2;
            this.cbo_hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cbobBoPhan_KeyDown);
            // 
            // frmInLyLich
            // 
            this.ClientSize = new System.Drawing.Size(295, 182);
            this.Controls.Add(this.dtp_denngay);
            this.Controls.Add(this.btnexel);
            this.Controls.Add(this.butThoat);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.butIn);
            this.Controls.Add(this.cbo_hoten);
            this.Controls.Add(this.cbobBoPhan);
            this.Controls.Add(this.butXem);
            this.Controls.Add(this.ckbToanCongTy);
            this.ForeColor = System.Drawing.Color.DarkBlue;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "frmInLyLich";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "In lý lịch";
            this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmInLyLich_MouseMove);
            this.Load += new System.EventHandler(this.frmInLyLich_Load);
            this.ResumeLayout(false);

		}
		#endregion

		private void butThoat_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void ckbToanCongTy_CheckedChanged(object sender, System.EventArgs e)
		{
			cbo_hoten.Enabled = !ckbToanCongTy.Checked;
			cbobBoPhan.Enabled = !ckbToanCongTy.Checked;
            cbo_hoten.SelectedIndex = -1;
			cbobBoPhan.SelectedIndex=-1;
			this.Size=new Size(320,192);
		}

		private void butXem_Click(object sender, System.EventArgs e)
		{
			if(ckbToanCongTy.Checked==false && cbobBoPhan.SelectedIndex==-1 && cbo_hoten.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Chưa chọn đối tượng cần xem"),AccessData.Msg,MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
			else
				print(1);
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			if(ckbToanCongTy.Checked==false && cbobBoPhan.SelectedIndex==-1 && cbo_hoten.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Chưa chọn đối tượng cần in"),AccessData.Msg,MessageBoxButtons.OK,MessageBoxIcon.Warning);
			}
			else
				print(1);
		}

		private void print(int prn)
		{
			try
			{
                ExportToExcel exp = new ExportToExcel();
                DataSet dstmp=new DataSet();
                m_user = mDB.user;
                string ngayht = DateTime.Now.ToString("yyyy-MM-dd", null);
                denngay = dtp_denngay.Text.Trim();
                s_mmyy = denngay.Substring(3, 2) + denngay.Substring(2, 2);
                sql = "select l.madv,l.manv,bp.tenbp,trim(l.ho)||' '||l.ten as Hovaten";
                sql += " ,case when l.phai='Nam' then to_char(l.ngaysinh,'dd/mm/yyyy') else null end as Ngaysinhnam";
				sql += " ,case when l.phai<>'Nam' then to_char(l.ngaysinh,'dd/mm/yyyy') else null end as Ngaysinhnu";
				sql += " ,trim(l.diachi)||', '||trim(qu.tenquan)||', '||trim(tt.tentt) as hk_thuongtru";
                sql += " ,trim(l.noio)||', '||trim(q.tenquan)||', '||trim(t.tentt) as Choohiennay,l.quequan";
                sql += " ,l.cmndso as SoCMND,l.noicapcmnd,to_char(l.cmndngay,'dd/mm/yyyy') as ngaycap";
				sql += " ,hv.tenhv as Hocvan,cd.ten as Chucdanh,hd.luonghd as Mucluong,hd.hs_lc,bp.sothutu as stt_bp,cd.thutu as stt_cd,cv.stt as stt_cv";
                sql += " ,cv.diengiai as chucvu,mn.diengiai as ngach_vc,dtoc.tendt as tendantoc,hd.sobhxh";
                sql += " ,to_char(hd.ngaydenct,'dd/mm/yyyy') as ngaytuyendung";
                sql += " ,trim(dt.tenctdt)||' - '||trim(bc.tenbang) as trinhdo_cm";
				sql += " ,to_char(now(),'dd') as ngay,to_char(now(),'mm') as thang,to_char(now(),'yyyy') as nam";
                sql += " from " + m_user + ".llnv l inner join " + m_user + ".hopdong hd on l.manv=hd.manv";
                sql += " left join " + m_user + ".bophan bp on l.madv=bp.madv";
                sql += " left join " + m_user + ".btdtt tt on l.t_diachi=tt.matt";
                sql += " left join " + m_user + ".btdquan qu on l.h_diachi=qu.maqu";
                sql += " left join " + m_user + ".btdtt t on l.t_noio=t.matt";
                sql += " left join " + m_user + ".btdquan q on l.h_noio=q.maqu";
                sql += " left join " + m_user + ".hocvan hv on l.mahv=hv.mahv";
                sql += " left join " + m_user + ".dmcd cd on l.macd = cd.ma";
                sql += " left join " + m_user + ".dmcv cv on hd.macv=cv.macv ";
                sql += " left join " + m_user + ".mangach mn on hd.mangach=mn.ma ";
                sql += " left join " + m_user + ".bangcap bc on l.td_nnghiep=bc.mabangcap ";
                sql += " left join " + m_user + ".ctdaotao dt on l.nnghiep=dt.mactdt";
                sql += " left join " + m_user + ".dantoc dtoc on l.madt=dtoc.madt ";
                sql += " inner join "+ m_user+".max_lan ml on hd.manv=ml.manv";
                
				if(ckbToanCongTy.Checked)
					sql+="";
				else 
				if (cbobBoPhan.SelectedIndex!=-1) 
					{
						sql+=" where l.madv='"+cbobBoPhan.SelectedValue.ToString()+"'";
                        if (cbo_hoten.SelectedIndex != -1)
                        {
                            sql += " and l.manv ='" + cbo_hoten.SelectedValue.ToString().Trim() + "'";
                        }
					}
                sql += " and hd.lan=ml.lan ";
                sql += " and (hd.ngaynghi is null or to_timestamp(hd.ngaynghi,'dd-mm-yyyy') > to_timestamp('"+denngay+"','dd/mm/yyyy'))";
                sql += " and to_timestamp(hd.ngaydenct,'dd-mm-yyyy') <= to_timestamp('" + denngay + "','dd/mm/yyyy')";
				sql += " order by bp.sothutu,cv.stt,cd.thutu,l.manv ";
				ds=mDB.get_data(sql);
                ds.WriteXml("..\\xml\\h_soyeulylich.xml", XmlWriteMode.WriteSchema);
				if (ds.Tables[0].Rows.Count>0)
				{
                    switch(prn)
                    {
                        case 1: frmReport f = new frmReport(mDB, ds,lan.Change_language_MessageText("SƠ YẾU LÝ LỊCH"), "h_soyeulylich.rpt",user_dn);
                            f.ShowDialog();
                            break;
                        case 2:dstmp=xuat_exel(ds);
                            exp.exel_lylichnv(dstmp, "soyeulylich",s_mmyy);
                            break;
                    }				
					ckbToanCongTy.Enabled=true;
					cbobBoPhan.Enabled=true;
					cbo_hoten.Enabled=true;
					ckbToanCongTy.Checked=false;
					cbobBoPhan.Text="";
					cbo_hoten.SelectedIndex=-1;						
				}
				else
				{
                    MessageBox.Show(lan.Change_language_MessageText("Chưa có đầy đủ lý lịch của nhân viên "), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
					ckbToanCongTy.Enabled=true;
					cbobBoPhan.Enabled=true;
					cbo_hoten.Enabled=true;
					ckbToanCongTy.Checked=false;
					cbobBoPhan.Text="";
                    cbo_hoten.SelectedIndex = -1;
				}
			}
			catch{}
		}
        private DataSet xuat_exel(DataSet dse)
        {
            string[] str = new string[] { "STT", "Họ Và Tên", "Nam", "Nữ", "Chỗ ở", "Số CMND", "Học Vấn", "Mức Lương", "Chức Danh" };
            DataSet dse1 = new DataSet();
            dse1.Tables.Add();
            dse1.Tables[0].Columns.Add("stt",typeof(string));
            dse1.Tables[0].Columns.Add("Hovaten", typeof(string));
            dse1.Tables[0].Columns.Add("Ngaysinhnam", typeof(string));
            dse1.Tables[0].Columns.Add("Ngaysinhnu", typeof(string));
            dse1.Tables[0].Columns.Add("Choohiennay", typeof(string));
            dse1.Tables[0].Columns.Add("SoCMND", typeof(string));
            dse1.Tables[0].Columns.Add("Ngaycap", typeof(string));
            dse1.Tables[0].Columns.Add("Noicap", typeof(string));
            dse1.Tables[0].Columns.Add("Hocvan", typeof(string));
            dse1.Tables[0].Columns.Add("Mucluong", typeof(string));
            dse1.Tables[0].Columns.Add("Chucdanh", typeof(string));
            decimal hs_lc=0, luonghd=0, mucluong = 0;
            int stt = 0;
            DataRow nrow;
            foreach (DataRow r in dse.Tables[0].Rows)
            {
                stt++;
                nrow = dse1.Tables[0].NewRow();
                hs_lc = decimal.Parse(r["hs_lc"].ToString());
                luonghd=decimal.Parse(r["Mucluong"].ToString());
                mucluong=hs_lc*luonghd;
                nrow["stt"] = stt;
                nrow["Hovaten"] = r["Hovaten"].ToString();
                nrow["Ngaysinhnam"] = r["Ngaysinhnam"].ToString();
                nrow["Ngaysinhnu"] = r["Ngaysinhnu"].ToString();
                nrow["Choohiennay"] = r["Choohiennay"].ToString();
                nrow["SoCMND"] = r["SoCMND"].ToString();
                nrow["Ngaycap"] = r["ngaycap"].ToString();
                nrow["Noicap"] = r["noicapcmnd"].ToString();
                nrow["Hocvan"] = r["Hocvan"].ToString();
                nrow["Mucluong"] = mucluong.ToString();
                nrow["Chucdanh"] = r["Chucdanh"].ToString();
               
                dse1.Tables[0].Rows.Add(nrow);
            }
            return dse1;
        }
		private void Load_BoPhan()
		{
			cbobBoPhan.DisplayMember="tenbp";
			cbobBoPhan.ValueMember="madv";
			cbobBoPhan.DataSource=mDB.get_bophan().Tables[0];
		}

		private void frmInLyLich_Load(object sender, System.EventArgs e)
		{            
            m_user = mDB.user;
            cbobBoPhan.Focus();
			Load_BoPhan();
			cbobBoPhan.SelectedIndex=-1;			
		}

		private void ckbToanCongTy_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}");
		}
		private void cbobBoPhan_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
				SendKeys.Send("{Tab}{F4}");
		}

		private void txbHoTenNV_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
			{
				SendKeys.Send("{Tab}");				
			}
		}

		private void frmInLyLich_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (bClear)
			{
				cbobBoPhan.SelectedIndex=-1;
				bClear=false;
			}
		}
        private void btnexel_Click(object sender, EventArgs e)
        {
            print(2);
        }

        private void cbobBoPhan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == this.cbobBoPhan)
            {
                load_nhanvien(cbobBoPhan.SelectedValue.ToString().Trim());
            }
        }
        private void load_nhanvien(string s_bophan)
        {
            sql = " select manv,trim(ho)||' '||trim(ten) as hoten ";
            sql+= " from " + m_user + ".llnv where madv='"+s_bophan.Trim()+"'";
            cbo_hoten.DataSource = mDB.get_data(sql).Tables[0];
            cbo_hoten.ValueMember = "manv";
            cbo_hoten.DisplayMember = "hoten";
            cbo_hoten.SelectedIndex = -1;
        }

        private void dtp_denngay_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}{f4}");
        }
	}
}
